package com.polaris.live.popup.party

import android.content.Context
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.PopupPartyLockBinding
import com.polaris.live.net.liveService
import com.polaris.live.resp.back_resp.LivePasswordResp
import com.polaris.live.translation.R.string
import com.polaris.live.viewmodel.party.PartyAnchorFragmentModel

/**
 *  房间上锁
 * @author Created by 天晴 on 2024/2/27 19:18
 * @since 1.0.0
 **/
class PartyLockPopup(
    context: Context,
    private var mPassword: String? = null,
    private val mViewModel: PartyAnchorFragmentModel,
    private val onLock: (lock: Boolean) -> Unit
) : CenterPopupView(context) {

    private lateinit var mBinding: PopupPartyLockBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_party_lock
    }

    /**
     * 0为修改密码界面  1为有密码展示界面
     */
    private var state = 0

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupPartyLockBinding.bind(popupImplView)
        state = if (mPassword == null) 0 else 1
        initView(state)
        initListener()
    }

    private fun initListener() {
        mBinding.apply {
            confirm.setOnSingleClickListener {
                if (state == 0) {
                    passwordView.getText()?.let {
                        setNetPassword(it)
                    }
                } else {
                    deleteNetPassword()
                }
            }
            cancel.setOnSingleClickListener {
                if (state == 0) {
                    dismiss()
                } else {
                    state = 0
                    initView(state)
                }
            }

        }

    }

    /**
     * 删除密码
     */
    private fun deleteNetPassword() {
        mViewModel.requestLoading(
            { liveService.deletePassword() },
            {
                ToastGravity.showGravity(string.qu_successfully)
                onLock.invoke(false)
                dismiss()
            },
            { ToastGravity.showGravity(it.errorMsg) })
    }

    /**
     * 设置密码
     */
    private fun setNetPassword(password: String) {
        mViewModel.requestLoading(
            { liveService.setPassword(LivePasswordResp(password)) },
            {
                ToastGravity.showGravity(string.set_successfully)
                onLock.invoke(true)
                dismiss()
            },
            { ToastGravity.showGravity(it.errorMsg) })
    }

    private fun initView(state: Int) {
        mBinding.apply {
            if (state == 0) {
                title.text = StringUtils.getString(string.set_room_password)
                confirm.text = StringUtils.getString(string.confirm)
                cancel.text = StringUtils.getString(string.cancel)
                passwordShow.text = ""
                passwordView.visibility = VISIBLE
                passwordShow.visibility = GONE
                mPassword?.let {
                    passwordView.setText(it)
                } ?: passwordView.requestOneFocus()

            } else if (state == 1) {
                title.text = StringUtils.getString(string.room_password)
                confirm.text = StringUtils.getString(string.unlock)
                cancel.text = StringUtils.getString(string.change_password)
                passwordShow.text = mPassword ?: ""
                passwordView.visibility = GONE
                passwordShow.visibility = VISIBLE
                passwordView.clearAllFocus()
            }
        }
    }
}